package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysCombo;
import com.ruoyi.system.domain.SysDishes;
import com.ruoyi.system.domain.SysEndCombo;
import com.ruoyi.system.service.ISyOnlyComboService;
import com.ruoyi.system.service.ISyThreeComboService;
import com.ruoyi.system.service.ISysTotalDishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/system/threeCombo")
public class SysThreeComboController extends BaseController {
    @Autowired
    private ISyThreeComboService syThreeComboService;
    @Autowired
    private ISysTotalDishesService sysTotalDishesService;

    /**
     * 获取全部套餐管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:combo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysEndCombo sysEndCombo){
        List<SysEndCombo> sysEndCombos = syThreeComboService.selectOnlyComDisList(sysEndCombo);
        return getDataTable(sysEndCombos);
    }

    /**
     * 修改套餐管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:combo:edit')")
    @Log(title = "套餐管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCombo sysCombo)
    {
        System.out.println(sysCombo);
        // 判断套餐编码是否符合格式限制
        String comboCode = sysCombo.getComboCode();
        String regex = "\\d+\\*\\d+(?:\\+\\d+\\*\\d+)*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(comboCode);
        if (matcher.matches()) {
            int check = syThreeComboService.updateSysComboById(sysCombo);
            if (check == 0){
                return AjaxResult.error("套餐中部分菜品下架，此套餐无法上架");
            }
            return toAjax(check);
        } else {
            return AjaxResult.error("您输入的套餐编码不正确哦~,重新输入或直接点击下方计数器增减菜品，系统将为你自动生成套餐编码哦~");
        }
    }

    /**
     * 获取套餐管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:combo:query')")
    @GetMapping(value = "/{comboDishesId}")
    public AjaxResult getInfo(@PathVariable("comboDishesId") Long comboDishesId)
    {
        return success(syThreeComboService.selectSysComboByComboDishesId(comboDishesId));
    }

    /**
     *新增套餐管理
     */
    @PreAuthorize("@ss.hasPermi('system:combo:add')")
    @Log(title = "套餐管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCombo sysCombo)
    {
        return toAjax(syThreeComboService.insertCombo(sysCombo));
    }


    /**
     *修改统一管理状态
     */
    @PreAuthorize("@ss.hasPermi('system:combo:edit')")
    @Log(title = "套餐管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysCombo sysCombo)
    {
        System.out.println(sysCombo);
        return toAjax(syThreeComboService.updateSysComboById(sysCombo));
    }

    /**
     * 获取全部菜品信息
     */
    @PreAuthorize("@ss.hasPermi('system:combo:allDishesList')")
    @GetMapping("allDishesList")
    public TableDataInfo allDishesList(){
        startPage();
        List<SysDishes> sysDishes = sysTotalDishesService.selectAllDishes();
        return getDataTable(sysDishes);
    }

    /**
     * 根据comboDishesId删除套餐
     */
    @PreAuthorize("@ss.hasPermi('system:combo:remove')")
    @Log(title = "套餐管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{comboDishesIds}")
    public AjaxResult remove(@PathVariable Long[] comboDishesIds)
    {
        return toAjax(syThreeComboService.deleteByComboDishesId(comboDishesIds));
    }

    /**
     * EXCEL
     */
    @PreAuthorize("@ss.hasPermi('system:combo:export')")
    @Log(title = "套餐管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEndCombo sysEndCombo)
    {
        List<SysEndCombo> sysEndCombos = syThreeComboService.selectOnlyComDisList(sysEndCombo);
        ExcelUtil<SysEndCombo> util = new ExcelUtil<SysEndCombo>(SysEndCombo.class);
        util.exportExcel(response, sysEndCombos, "菜品管理数据");
    }
}
